package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.dao.OrdersDao;
import com.reggie.pojo.Orders;
import com.reggie.service.OrdersService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersDao dao;
    @Override
    public void addOrders(Orders orders) {
        dao.insert(orders);
    }

    @Override
    public Page<Orders> selectByPage(Integer page, Integer pageSize, HttpServletRequest request) {
        Page<Orders> pages = new Page<>(page,pageSize);
        Long user_id = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<Orders> lqw= new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,user_id);
        lqw.orderByDesc(Orders::getOrderTime);
        dao.selectPage(pages,lqw);
        return pages;
    }

    public Page<Orders> selectByPageAll(Integer page, Integer pageSize, Long number, String beginTime,String endTime){
        List<Orders> orders = dao.selectList(null);
        orders.forEach(item -> {
            item.setNumber(String.valueOf(item.getId()));
            dao.deleteById(item.getId());
            dao.insert(item);
        });
        Page<Orders> pages = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(number != null,Orders::getNumber,number);
        lqw.between(beginTime != null || endTime != null,Orders::getOrderTime,beginTime,endTime);
        dao.selectPage(pages,lqw);
        return pages;
    }
}
